<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/24 0024
 * Time: 17:36
 */
class Http
{
    protected $host = '192.168.3.221';
    protected $port = 8811;

    public $http = null;
    public function __construct()
    {
        $this->http = new swoole_http_server($this->host, $this->port);

        //设置静态文件的访问路径
        $this->http->set([
            'worker_num' => 4,
            'task_worker_num' => 4,
            'enable_static_handler' => true,
            'document_root' => '/data/wwwroot/www/swoole-demo/mooc/tp5/public/static',
        ]);
        $this->http->on('workerstart', [$this, 'onWorkerStart']);
        $this->http->on('request', [$this, 'onRequest']);
        $this->http->on('task', [$this, 'onTask']);
        $this->http->on('finish', [$this, 'onFinish']);
        $this->http->on('close', [$this, 'onClose']);

        $this->http->start();
    }

    /**
     * workerstart 回调
     * @param $server
     * @param $worker_id
     */
    public function onWorkerStart($server, $worker_id)
    {
        //加载tp框架的内容
//        require __DIR__ . '/../thinkphp/base.php';
        require __DIR__ . '/../public/index.php';
    }

    /**
     * request 回调
     * @param $request
     * @param $response
     */
    public function onRequest($request, $response)
    {
        $response->header("Content-Type", "text/html;charset=utf-8");
        $_GET = [];
        $_POST = [];
        if(isset($request->get)){
            foreach ($request->get as $k => $v){
                $_GET[$k] = $v;
            }
        }
        if(isset($request->post)){
            foreach ($request->post as $k => $v){
                $_POST[$k] = $v;
            }
        }
        if(isset($request->server)){
            foreach ($request->server as $k => $v){
                $_SERVER[strtoupper($k)] = $v;
            }
        }

        //赋值，在其他地方使用
        $_POST['http_server'] = $this->http;

        ob_start();
        // 执行应用并响应
        try{
            //run() 方法在thinkphp/library/think/app.php
            think\Container::get('app')->run()->send();
        }catch(\Exception $e){
            //todo
        }
        $res = ob_get_contents();
        ob_end_clean();
        $response->end($res);
    }

    /**
     * 任务
     * @param $serv swoole_server对象
     * @param $task_id  任务id
     * @param $worker_id worker进程id
     * @param $data 任务的内容
     */
    public function onTask($serv, $task_id, $worker_id, $data)
    {
        //分发 task 任务机制，让不同的任务走不同的逻辑
        $obj = new \app\common\lib\task\Task();
        //分发任务
        $method = $data['method'];
        $flag = $obj->$method($data['data']);

        return $flag;   //告诉worker，调finish
    }

    /**
     * 结束任务
     * @param $serv
     * @param $task_id  任务id
     * @param $data 任务处理的结果内容,ontask方法return的内容
     */
    public function onFinish($serv, $task_id, $data)
    {
        echo "takdId: {$task_id}\n";
        echo "finish-data-success:{$data}\n";
//        $serv->push($data, '5s后的数据');
    }

    /**
     * 监听ws关闭事件
     * @param $ws
     * @param $fd
     */
    public function onClose($http, $fd)
    {
//        echo "clientid:{$fd}\n";
    }
}

new Http();




